﻿using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;

public class SkyboxManager : MonoBehaviour
{
    public Material[] Skyboxs;
    public float speed = 0.1F;//speed


    public List<string> skyName = new List<string>(names);
    private static string[] names = { "sunset", "night", "bluesky", "rain", "dusk", "sunrise", "haze" };
    int index;

    private Skybox sky;

    private float value;

    // Start is called before the first frame update
    void Start()
    {
        index = 0;
        sky = transform.GetComponent<Skybox>();
        value = 0;
    }
    void ChangeSkyBox()
    {
        sky.material = Skyboxs[index];
        index++;
        index %= Skyboxs.Length;
    }
    void ChangeSkyBox(int index)
    {
        sky.material = Skyboxs[index];
    }

    /// <summary>
    /// change weather, return success tag
    /// </summary>
    /// <param name="skyname"></param>
    /// <returns></returns>
    public bool ChangeSkybox(string skyname)
    {
        if (skyName.Contains(skyname)) {
            ChangeSkyBox(skyName.IndexOf(skyname));
            return true;
        }
        else
        {
            return false;
        }
    }



    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            ChangeSkyBox();
        }

        if (sky.material != null)
        {
            value += Time.deltaTime * speed;
            sky.material.SetFloat("_Rotation", value);
        }
    }

    public GameObject[] Weathers;
    public List<string> weatherName = new List<string>(new string[] { "rain", "snow" });
    private GameObject current_weather;

    //public Dictionary<string, string> weatherName = new Dictionary<string, string>{{"snow","asdawds"}};
    public bool ChangeWeather(string weather)
    {
        if (weatherName.Contains(weather))
        {
            try
            {
                GameObject obj = Weathers[weatherName.IndexOf(weather)];
                obj.name = weather;

                if (current_weather != null)
                    GameObject.Destroy(current_weather);
                current_weather = Instantiate(obj);
            }
            catch (Exception e)
            {
                TextDialog.textDebug("weather doesn't exist!!!");
            }
        }
        else if(weather.Contains("sunny"))
        {
            if (current_weather != null)
                GameObject.Destroy(current_weather);
        }
        else{

            TextDialog.textDebug("weather doesn't exist!!!");

            return false;
        }

        return true;
    }


}